<!doctype html>
<html class="no-js" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>
    
  iOS 架构模式学习笔记 - kaelinda
  
  </title>
  
  
  <link href="atom.xml" rel="alternate" title="kaelinda" type="application/atom+xml">
    <link rel="stylesheet" href="asset/css/foundation.min.css" />
    <link rel="stylesheet" href="asset/css/docs.css" />
    <script src="asset/js/vendor/modernizr.js"></script>
    <script src="asset/js/vendor/jquery.js"></script>
  <script src="asset/highlightjs/highlight.pack.js"></script>
  <link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
  <script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
  function before_search(){
    var searchVal = 'site:kaelinda.top ' + document.getElementById('search_input').value;
    document.getElementById('search_q').value = searchVal;
    return true;
  }
</script>
  </head>
  <body class="antialiased hide-extras">
    
    <div class="marketing off-canvas-wrap" data-offcanvas>
      <div class="inner-wrap">


<nav class="top-bar docs-bar hide-for-small" data-topbar>


  <section class="top-bar-section">
  <div class="row">
      <div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
        <ul id="main-menu" class="left">
        
        <li id=""><a target="_self" href="index.html">最近更新</a></li>
        
        <li id=""><a target="_self" href="archives.html">归档目录</a></li>
        
        <li id="ios.css"><a target="_self" href="ios.html">iOS</a></li>
        
        <li id="swift.css"><a target="_self" href="swift.html">Swift</a></li>
        
        <li id="reactnative.css"><a target="_self" href="reactnative.html">RN</a></li>
        
        <li id="js.css"><a target="_self" href="js.html">JS</a></li>
        
        <li id="shell.css"><a target="_self" href="shell.html">shell</a></li>
        
        <li id="coding.css"><a target="_self" href="coding.html">编程</a></li>
        
        <li id="live.css"><a target="_self" href="live.html">随笔</a></li>
        
        <li id="app.css"><a target="_blank" href="app.html">APP推荐</a></li>
        
        </ul>

        <ul class="right" id="search-wrap">
          <li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
    <input type="hidden" id="search_q" name="q" value="" />
    <input tabindex="1" type="search" id="search_input"  placeholder="Search"/>
</form>
</li>
          </ul>
      </div></div>
  </div>
  </section>

</nav>

        <nav class="tab-bar show-for-small">
  <a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
    <span> &nbsp; kaelinda</span>
  </a>
</nav>

<aside class="left-off-canvas-menu">
      <ul class="off-canvas-list">
        
        <li><a target="_self" href="index.html">最近更新</a></li>
        
        <li><a target="_self" href="archives.html">归档目录</a></li>
        
        <li><a target="_self" href="ios.html">iOS</a></li>
        
        <li><a target="_self" href="swift.html">Swift</a></li>
        
        <li><a target="_self" href="reactnative.html">RN</a></li>
        
        <li><a target="_self" href="js.html">JS</a></li>
        
        <li><a target="_self" href="shell.html">shell</a></li>
        
        <li><a target="_self" href="coding.html">编程</a></li>
        
        <li><a target="_self" href="live.html">随笔</a></li>
        
        <li><a target="_blank" href="app.html">APP推荐</a></li>
        

    <li><label>Categories</label></li>

        
            <li><a href="reactnative.html">React-Native</a></li>
        
            <li><a href="ios.html">iOS</a></li>
        
            <li><a href="app.html">MacAPP</a></li>
        
            <li><a href="swift.html">Swift</a></li>
        
            <li><a href="js.html">JavaScript</a></li>
        
            <li><a href="vue.html">Vue</a></li>
        
            <li><a href="shell.html">shell</a></li>
        
            <li><a href="%E7%BD%91%E7%BB%9C.html">网络</a></li>
         

      </ul>
    </aside>

<a class="exit-off-canvas" href="#"></a>


        <section id="main-content" role="main" class="scroll-container">
        
       

 <script type="text/javascript">
  $(function(){
    $('#menu_item_index').addClass('is_active');
  });
</script>
<div class="row">
  <div class="large-8 medium-8 columns">
      <div class="markdown-body article-wrap">
       <div class="article">
          
          <h1>iOS 架构模式学习笔记</h1>
     
        <div class="read-more clearfix">
          <span class="date">2018/7/24</span>

          <span>posted in&nbsp;</span> 
          
              <span class="posted-in"><a href='ios.html'>iOS</a></span>
           
         
          <span class="comments">
            

            
          </span>

        </div>
      </div><!-- article -->

      <div class="article-content">
      <h2 id="toc_0">设计模式</h2>

<p>设计模式 和 编码技巧 、架构模式的区别：</p>

<ul>
<li><p>设计模式：特定场景下的最佳实践 如： MVC MVVM MVP。</p></li>
<li><p>编码技巧：单例模式、工厂模式等</p></li>
<li><p>架构模式：包括网络模块实现、数据存储模块实现、业务逻辑模块实现分离复用等</p></li>
</ul>

<blockquote>
<p><strong>架构模式  &gt; 设计模式 &gt; 编码技巧</strong></p>
</blockquote>

<span id="more"></span><!-- more -->

<ul>
<li>继承</li>
<li>关联</li>
<li>组合</li>
<li>聚合</li>
</ul>

<h3 id="toc_1">MVC</h3>

<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1ftrut0kos5j318q0v8dj6.jpg" alt="MVC"/></p>

<ul>
<li>用户触发View交互，传递数据给Controller，controller接收UI更改信号去更改Model，再将Model传递给View 更新View；</li>
<li>当然 如有特殊需求 也能实现一些反向的数据通信：View  更改Model，传递给Controller，controller去更改View</li>
</ul>

<p><strong>由此可见：整个UI更新过程，数据是单向(环状)传递的</strong></p>

<p>环状这就导致了，类与类之间交互复杂，业务逻辑的<strong>耦合度</strong>会很高</p>

<h4 id="toc_2">解重</h4>

<ul>
<li>Present：处理数据初始化、数据组合、数据过滤  UI 交互传值 delegate</li>
<li>dataSource：传入CellIdentity ，以及model</li>
</ul>

<p>Controller  跟present交互<br/>
Model 跟 present交互<br/>
present作为中心逻辑处理</p>

<h4 id="toc_3">解耦</h4>

<ul>
<li>子UI组件 刷新UI的时候大都会有这个方法:</li>
</ul>

<pre><code class="language-text">-(void) loadModel:(BaseModel *)model;
</code></pre>

<p>这样，每次在复用UI组件的时候不得已，可能还得复用<code>Model</code>。如果跨项目，复用<code>Model</code>就可能不太合理了，那么我们在刷新UI的时候 尽可能的载入进本的更该数值 而并非 笨重的<code>Model</code>。</p>

<ul>
<li>MVC 中环形的数据传递，必然导致 文件的相互包含，相互引用、相互依赖问题，MVP模式中把依赖都挪到了present 是一个比较好的选择。</li>
</ul>

<h3 id="toc_4">MVP</h3>

<p><img src="https://ws4.sinaimg.cn/large/006tKfTcly1ftruta5qp1j31840tygpy.jpg" alt="MVP"/></p>

<ul>
<li>优点： View组件很薄，处理的逻辑很少，不需要持有任何的Model文件的引用，复用的时候直接copy   View组件进去就能用；传统的MVC中都是Model去驱动View ，View组件中都会引用Model，耦合度高改了model就得改View ，改了View ，model也可能需要更改。</li>
<li>缺点：present 作为中间人，需要交互的内容太多，逻辑很重</li>
</ul>

<h3 id="toc_5">MVVM</h3>

<p><img src="https://ws1.sinaimg.cn/large/006tKfTcly1ftrutkkon8j317a0tu78o.jpg" alt="MVVM"/></p>

<ul>
<li>MVVM：<br/>
Model 、View 依旧是互不通信，跟MVP类似，<br/>
唯一不同就是View和<code>ViewModel</code>是双向绑定的关系，并非双向通信，比双向通信关系更紧密<code>ViewModel</code>跟  model 是双向通信的关系，没有<code>ViewModel</code> 跟View关系那么紧密</li>
</ul>

<h2 id="toc_6">总结</h2>

<p>MVP 、MVVM 其实都是MVC发展而来的，三种设计模式没有孰优孰劣，只有哪个更适合。</p>

<p>个人总结对比：</p>

<ul>
<li>MVC：相对灵活，简单，容易掌握，数据传递比较直接。更适合轻量级的项目。</li>
<li>MVP：有了一定的规则，present 集中处理业务逻辑，一定程度上减少了耦合度，中小型项目都还比较合适</li>
<li>MVVM：写起来听复杂的，每个View对应一个ViewModel，对于成型的稳定的大项目来说，还是很适合的，一定程度上降低了耦合度不会直接操作View  和 Model 而是操作ViewModel 去更改View 更改Model，耦合度低，较为灵活。 Model 、View、ViewModel三者之间关系以及各自作用熟悉了以后，还是很清晰的。</li>
</ul>


    

      </div>

      <div class="row">
        <div class="large-6 columns">
        <p class="text-left" style="padding:15px 0px;">
      
          <a href="15329136223185.html" 
          title="Previous Post: iOS 奇怪问题搜集栈">&laquo; iOS 奇怪问题搜集栈</a>
      
        </p>
        </div>
        <div class="large-6 columns">
      <p class="text-right" style="padding:15px 0px;">
      
          <a  href="15311162986405.html" 
          title="Next Post: iOS tableView 滚动到指定行/段  以及遇到的bug 指南">iOS tableView 滚动到指定行/段  以及遇到的bug 指南 &raquo;</a>
      
      </p>
        </div>
      </div>
      <div class="comments-wrap">
        <div class="share-comments">
          

          

          
        </div>
      </div>
    </div><!-- article-wrap -->
  </div><!-- large 8 -->




 <div class="large-4 medium-4 columns">
  <div class="hide-for-small">
    <div id="sidebar" class="sidebar">
          <div id="site-info" class="site-info">
            
                <div class="site-a-logo"><img src="https://ws1.sinaimg.cn/large/006tNc79ly1fmpk9088gsj30sg0sgn33.jpg" /></div>
            
                <h1>kaelinda</h1>
                <div class="site-des">倘若我心中的山水，你眼中能看到，我便一步一莲花祈祷</div>
                <div class="social">








<a target="_blank" class="twitter" target="_blank" href="https://twitter.com/KaelLVLinda" title="Twitter">Twitter</a>
<a target="_blank" class="github" target="_blank" href="https://github.com/Kaelzzs" title="GitHub">GitHub</a>
<a target="_blank" class="email" href="mailto:zhouzuosong_kael@163.com" title="Email">Email</a>
  <a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
                
              	 </div>
          	</div>

             

              <div id="site-categories" class="side-item ">
                <div class="side-header">
                  <h2>Categories</h2>
                </div>
                <div class="side-content">

      	<p class="cat-list">
        
            <a href="reactnative.html"><strong>React-Native</strong></a>
        
            <a href="ios.html"><strong>iOS</strong></a>
        
            <a href="app.html"><strong>MacAPP</strong></a>
        
            <a href="swift.html"><strong>Swift</strong></a>
        
            <a href="js.html"><strong>JavaScript</strong></a>
        
            <a href="vue.html"><strong>Vue</strong></a>
        
            <a href="shell.html"><strong>shell</strong></a>
        
            <a href="%E7%BD%91%E7%BB%9C.html"><strong>网络</strong></a>
         
        </p>


                </div>
              </div>

              <div id="site-categories" class="side-item">
                <div class="side-header">
                  <h2>Recent Posts</h2>
                </div>
                <div class="side-content">
                <ul class="posts-list">
	      
		      
			      <li class="post">
			        <a href="15523077857608.html">OC中枚举写法  以及 字符串枚举探索</a>
			      </li>
		     
		  
		      
			      <li class="post">
			        <a href="15522686353535.html">iOS 动画全解 (1)</a>
			      </li>
		     
		  
		      
			      <li class="post">
			        <a href="15501356584731.html">UITableView顶部空白的几种解决办法</a>
			      </li>
		     
		  
		      
			      <li class="post">
			        <a href="15488351784332.html">Objective-C中的字符串类型枚举 探索</a>
			      </li>
		     
		  
		      
			      <li class="post">
			        <a href="15409691048860.html">【Swift笔记】引用计数</a>
			      </li>
		     
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		  
		      
		   
		  		</ul>
                </div>
              </div>
        </div><!-- sidebar -->
      </div><!-- hide for small -->
</div><!-- large 4 -->

</div><!-- row -->

 <div class="page-bottom clearfix">
  <div class="row">
   <p class="copyright">Copyright &copy; 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,&nbsp; 
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
  </div>
</div>

        </section>
      </div>
    </div>

  
    

    <script src="asset/js/foundation.min.js"></script>
    <script>
      $(document).foundation();
      function fixSidebarHeight(){
        var w1 = $('.markdown-body').height();
          var w2 = $('#sidebar').height();
          if (w1 > w2) { $('#sidebar').height(w1); };
      }
      $(function(){
        fixSidebarHeight();
      })
      $(window).load(function(){
          fixSidebarHeight();
      });
     
    </script>

    
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>


  </body>
</html>
